package com.ysq.nfd.spider;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import org.apache.commons.io.IOUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.ysq.nfd.config.BaseConfigInfo;
import com.ysq.nfd.util.CommonUtil;

public class NetFileDownload {

	int timeout = 10000;
	
	String userAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64)";

	/**
	 * 下载网络文件
	 * 
	 * @param fileUrl
	 * @param savePath
	 */
	public void downloadFile(String fileUrl, String savePath) {

		if (!new File(savePath).getParentFile().exists()) {
			new File(savePath).getParentFile().mkdirs();
		}

		InputStream inStream = null;
		FileOutputStream outputStream = null;

		try {
			URL url = new URL(fileUrl);
			URLConnection conn = url.openConnection();

			HttpURLConnection httpURLConnection = (HttpURLConnection) conn;

			httpURLConnection.setConnectTimeout(timeout);

			// 防止屏蔽程序抓取而返回403错误
			httpURLConnection.setRequestProperty("User-Agent", userAgent);

			inStream = httpURLConnection.getInputStream();
			outputStream = new FileOutputStream(savePath);

			byte[] tmpBytes = new byte[1204];
			int index = 0;
			
			int sumbytes = 0;
			int readLength = 0;
			while ((readLength = inStream.read(tmpBytes)) != -1) {
				
				sumbytes = sumbytes + readLength;
				
				outputStream.write(tmpBytes, 0, readLength);
			}
			
			if (index++ % 500 == 0) {
				System.out.println(fileUrl + " | " + sumbytes);
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			IOUtils.closeQuietly(outputStream);
			IOUtils.closeQuietly(inStream);
		}
	}

	/**
	 * 
	 */
	public void spiderUrl(String url, String dirPath) {
		
		Document doc = null;
		try {
			doc = Jsoup.connect(url)
					.userAgent(userAgent)
					.timeout(timeout).get();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		try {
			CommonUtil.createPath(dirPath);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		Element preEle = doc.select("pre").first();
		
		Elements nodeList = preEle.children();
		
		boolean imgGet = false;

		int i = 0;
		while(i < nodeList.size()){
			Element oneNode = nodeList.get(i);
			
			String name = oneNode.nodeName();
			if("img".equalsIgnoreCase(name)){
				String argAlt = oneNode.attr("alt");
				if(!imgGet){
					
					if("[PARENTDIR]".equalsIgnoreCase(argAlt)){
						imgGet = true;
						
						i++;
					}
				}else{
					
					//再获取 a
					i++;
					Element hrefNode = nodeList.get(i);
					
					String fileName = hrefNode.text();
					String absHref = hrefNode.attr("abs:href");
					
					String fullPath = CommonUtil.getFullPath(dirPath, fileName);
					
					if("[DIR]".equalsIgnoreCase(argAlt)){//文件夹
						
						spiderUrl(absHref, fullPath);
						
					}else{
						
						downloadFile(absHref, fullPath);
						
					}
				}
			}
			
			i++;
		}
		
	}
	
	public void startDownload(){

		//首先创建文件夹
		String net_url = BaseConfigInfo.net_url;
		String save_path = BaseConfigInfo.save_path;
		
		spiderUrl(net_url, save_path);
		
	}
	
	public static void main(String[] args) {
		
		NetFileDownload netDown = new NetFileDownload();
		
		netDown.startDownload();
		
		System.out.println("finished ... ");
		
		
	}

}
